package servlet;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
import model.User;
import service.UserService;

import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name="UserLoginServlet",value = "/UserLoginServlet")
public class UserLoginServlet extends HttpServlet {
    private UserService uService=new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        //告诉浏览器，服务器发送的消息体的数据的编码
        response.setHeader("content-type", "text/html;charset=utf-8");
        String account=request.getParameter("account");
        String password =request.getParameter("password");
        PrintWriter pw =response.getWriter();
        User user= uService.Login(account,password);
//        System.out.println(user+"2131231231");//测试
        if(user==null){
            pw.print("<script>alert('账号或者/密码错误~~~~'); window.location='login.jsp' </script>");
        }else {
//            System.out.println(user);//测试
            request.getSession().setAttribute("user",user);
            pw.print("<script>alert('登录成功,欢迎您~!"+user.getAccount()+"'); window.location='index.jsp' </script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
